// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Record from '../views/Record.vue'
import Activity from '../views/Activity.vue'
import User from '../views/User.vue'
import Award from '../views/Award.vue'
import AwardItem from '../views/AwardItem.vue'
import AwardEdit from '../views/award/Edit.vue'
import AwardItemEdit from '../views/awarditem/Edit.vue'

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: 'record',
        children: [
            {
                path: '/record',
                name: 'record',
                component: Record,
            },
            {
                path: '/activity',
                name: 'activity',
                component: Activity,
            },
            {
                path: '/user',
                name: 'user',
                component: User,
            },
            {
                path: '/award',
                name: 'award',
                component: Award,
            },
            {
                path: '/award/:id',
                name: 'award-edit',
                component: AwardEdit,
            },
            {
                path: '/awarditem',
                name: 'awarditem',
                component: AwardItem,
            },
            {
                path: '/awarditem/:id',
                name: 'awarditem-edit',
                component: AwardItemEdit,
            },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {

    if (to.path === '/login') return next()

    if (localStorage.getItem('token')) {
        next()
    }
    else {
        return next('/login')
    }

    next()
})

export default router
